//节点
class Node{
    constructor(element){
        this.element = element;
        this.prev = null;
        this.next = null;
    }
}

//双向链表
class DoubleList{
    constructor(){
        this.head = null; //记录头元素
        this.tail = null; //记录尾元素
        this.count = 0;   //记录节点/元素的数量
    }

    getElementAt(index){
        let current = this.head;
        for(let i = 0;i<index;i++){
            current = current.next;
        }
        return current
    }

    //添加
    push(element){
        //创建节点
        let node = new Node(element);
        //判断链表是否为空
        if(this.head === null){
            //1.链表为空
            this.head = node;
            this.tail = node;
        }else{
            //2.链表不为空
            let last = this.tail;
            last.next = node;
            node.prev = last;
            this.tail = node;
        }
        this.count++;
    }

    //插入 
    insert(element,index){
        //1.边界值的判断
        if(index >= 0 && index <= this.count){
            //创建节点
            let node = new Node(element);
            if(index === 0){
                //1.开头插入
                if(this.head === null){
                    //1.1链表为空
                    this.head = node;
                    this.tail = node;
                }else{
                    //1.2链表不为空
                    let current = this.head;
                    node.next = current;
                    current.prev = node;
                    this.head = node;
                }
            }else if(index === this.count){
                //2.尾部插入    
                let last = this.tail;
                last.next = node;
                node.prev = last;
                this.tail = node;
            }else{
                //3.中间插入
                let current = this.getElementAt(index);
                current.prev.next = node;
                node.prev = current.prev;

                current.prev = node;
                node.next = current;
            }
            this.count++;
        }else{
            return false
        }
    }

    //删除
    removeAt(index){
        if(index >= 0 && index < this.count){
            let current = this.head;
            if(index === 0){
                //1.从开头删除
                if(this.count === 1){
                    this.head = null;
                    this.tail = null;
                }else{
                    this.head = current.next;
                    current.next.prev = null;
                }
            }else if(index === (this.count-1)){
                //2.从末尾删除
                current = this.tail;
                current.prev.next = null;
                this.tail = last.prev;
            }else{
                //3.从中间删除
                current = this.getElementAt(index);
                current.prev.next = current.next;
                current.next.prev = current.prev;
            }
            this.count--;
            return current.element
        }else{
            return undefined
        }
    }

    //下标
    indexOf(element){
        let current = this.head;
        for(let i = 0;i<this.count;i++){
            if(current.element === element){
                return i
            }
            current = current.next;
        }
        return -1
    }

    //删除
    remove(element){
        let index = this.indexOf(element);
        this.removeAt(index)
    }

    //获取头元素
    getHead(){
        return this.head
    }

    //获取尾元素
    getTail(){
        return this.tail
    }

    //获取链表个数
    size(){
        return this.count
    }

    //链表是否为空
    isEmpty(){
        return this.count === 1
    }

    //清空链表
    clear(){
        this.tail = null;
        this.head = null;
        this.count = 0;
    }
}

let d = new DoubleList();
d.push('a');

d.push('b');

d.push('c');

// d.insert('z',0)

// d.insert('q',2);

// console.log(d.indexOf('a0'));

// d.remove('a');

console.log(d.getHead());